package tristate
{
	import flash.errors.IllegalOperationError;
		
	public final class TsState
	{
		public static const Unchecked:TsState = new TsState("Unchecked");
		public static const Checked:TsState = new TsState("Checked");
		public static const Circled:TsState = new TsState("Circled");
		
		private var m_value:String;
		
		private static var locked:Boolean = false;
		
		// static initialization block to lock the class and prevent any 
		// further instantiation.
	    {
	      locked = true;
	    }
	    
	    // AS3 doesn't allow private constructors!!!
		public function TsState(value:String = null):void
		{
			if(locked)
			{
				throw new IllegalOperationError("You can't instantiate TsState");
			}
			m_value = value;
		}
		
		public function toString():String
		{
			return m_value;
		}
		
		public function toInt():int
		{
			switch(this)
			{
				case Unchecked:
					return 0;
				case Checked:
					return 1;
				case Circled:
					return 2;
			}
			// can't ever get here, but to avoid a compile error:
			return 0;
		}

		public static function fromXML(xml:XMLList):TsState
		{
			var n:int = parseInt(xml.toString());
			switch(n)
			{
				case 0:
					return Unchecked;
				case 1:
					return Checked;
				case 2:
					return Circled;
				default:
					throw new RangeError("TsState value must be 0, 1, or 2");
			}
		}
	}
}